1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sf.webphotos.locator;
17
18 import java.util.Hashtable;
19 import javax.naming.Context;
20 import javax.naming.InitialContext;
21 import javax.naming.NamingException;
22 import org.apache.log4j.Logger;
23
24
25
26
27
28 public class BasicEJBLocator {
29
30
31
32
33 protected InitialContext cachedContext = null;
34
35
36
37 public static String EJB_URL_PREFIXES = "org.jboss.naming";
38
39
40
41 public static String EJB_CONTEXT_FACTORY = "org.jnp.interfaces.NamingContextFactory";
42
43
44
45 public static String EJB_SERVER = "127.0.0.1:1099";
46
47 private static Logger logger = Logger.getLogger(BasicEJBLocator.class);
48
49
50
51
52 public void initEJBContext() {
53 try {
54 cachedContext = (InitialContext) getRemoteEJBsInitialContext();
55 } catch (NamingException e) {
56 logger.error("Erro fatal com context de EJBs", e);
57 }
58 }
59
60
61
62
63
64 public InitialContext getEJBContext() {
65 if (cachedContext == null) {
66 initEJBContext();
67 }
68 return cachedContext;
69 }
70
71
72
73
74
75
76 public Context getRemoteEJBsInitialContext() throws NamingException {
77 Hashtable<String, String> props = new Hashtable<String, String>();
78 props.put(Context.INITIAL_CONTEXT_FACTORY, EJB_CONTEXT_FACTORY);
79 props.put(Context.PROVIDER_URL, EJB_SERVER);
80 props.put(Context.URL_PKG_PREFIXES, EJB_URL_PREFIXES);
81 return new InitialContext(props);
82 }
83
84 }